<?php
/**
 * Created by PhpStorm.
 * User: 张良
 * Date: 2018/1/15
 * Time: 10:24
 */

namespace App\Http\Controllers\User;

use App\Http\Controllers\CommonController;
use App\Models\Ipbuy;
use App\Models\Ipsell;
use App\Models\Product;
use Illuminate\Support\Facades\Session;
use Request;
use DB;
use Redirect;

class IPController extends CommonController
{
    protected $second_type_table = "second_type";
    protected $pageNum = 15;
    public function IpPurchase()
    {
      /*  $IP_pay = object_to_array(DB::table('ipbuy')->
        join('first_type', 'ipbuy.first_type', '=', 'first_type.id')->
        join('second_type', 'ipbuy.second_type', '=', 'second_type.id')->
        get(['ipbuy.*', 'first_type.id as first_type_id', 'first_type.name as first_type',
            'second_type.id as second_type_id', 'second_type.name as second_type']));
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        $second_type = object_to_array(DB::table('second_type')->get(['id', 'name']));
        if (!empty($IP_pay)) {
            for ($i = 0; $i < count($IP_pay); $i++) {
                if ($IP_pay[$i]['progress'] == '1') {
                    $IP_pay[$i]['progress_name'] = '卖方同意';
                } elseif ($IP_pay[$i]['progress'] == '2') {
                    $IP_pay[$i]['progress_name'] = '买方同意';
                } elseif ($IP_pay[$i]['progress'] == '3') {
                    $IP_pay[$i]['progress_name'] = '合同签订';
                } elseif ($IP_pay[$i]['progress'] == '4') {
                    $IP_pay[$i]['progress_name'] = '手续办理';
                } elseif ($IP_pay[$i]['progress'] == '5') {
                    $IP_pay[$i]['progress_name'] = '办理成功';
                } elseif ($IP_pay[$i]['progress'] == '6') {
                    $IP_pay[$i]['progress_name'] = '转让凭证寄送';
                } elseif ($IP_pay[$i]['progress'] == '7') {
                    $IP_pay[$i]['progress_name'] = '等待处理';
                } elseif ($IP_pay[$i]['progress'] == '8') {
                    $IP_pay[$i]['progress_name'] = '无法购买';
                }
            }
        }
        $data = [
            'IP_pay' => $IP_pay,
            'first_type' => $first_type,
            'second_type' => $second_type
        ];
//                dump($IP_pay);exit();
        return view('user/ip/IpPurchase')->with('data', $data);*/
      $uid=Session::get('uid');
      $product=Ipbuy::with(['FirstType','SecondType'])
          ->where(['user_id'=>$uid])
          ->paginate($this->pageNum);
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        $data = ['product','first_type'];
        //dump($product);exit;
        return view('user/ip/IpPurchase', compact($data));
    }
//    public function IpPurchaseSearch()
//    {
//        $uid = Session::get('uid');
//        $request = Request::all();
//        $keywords="";
//        if (isset($request['search'])) {
//            $keywords = $request['search'];
//        }
//        $uid=Session::get('uid');
//        $product=Ipbuy::with(['FirstType','SecondType'])
//            ->where(['user_id'=>$uid])
//            ->paginate($this->pageNum);
//        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
//        $data = ['product','first_type','keywords'];
//        //dump($product);exit;
//        return view('user/ip/IpPurchaseSearch', compact($data));
//    }

    public function IpPurchaseCertain()
    {
        $request = Request::all();
        $uid=Session::get('uid');
        $first_type = $request['first_type'];
        $second_type = $request['second_type'];
        $name = $request['name'];
        $created_at = $request['created_at'];
        $progress = $request['progress'];
        $arr_province = ["不填","专利","商标","版权","项目"];
        $arr_second_type=config('config.arr_second_type');
        for ($j = 0;$j < sizeof($arr_second_type);$j++){
            if ($second_type == $arr_second_type[$j]){
                if ($j==0)
                    $j='';
                break;
            }
        }
        for ($i = 0;$i < 5;$i++){
            if ($first_type == $arr_province[$i]){
                if ($i==0)
                    $i='';
                break;
            }
        }
       // dd($request);
        $product=Ipbuy::with(['FirstType','SecondType'])
            ->when($i, function ($query) use ($i) {
                return $query->where('first_type', $i);
            })
            ->when($j, function ($query) use ($j) {
                return $query->where('second_type', $j);
            })
            ->when($name, function ($query) use ($name) {
                return $query->where('name', $name);
            })
            ->when($created_at, function ($query) use ($created_at) {
                return $query->where('created_at','like','%'.$created_at.'%');
            })
            ->when($progress, function ($query) use ($progress) {
                return $query->where('progress', $progress);
            })
            ->where(['user_id'=>$uid])
            ->paginate($this->pageNum);
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        $data = ['product','first_type'];
        return view('user/ip/IpPurchaseSearch', compact($data));
    }

    public function getSecondType()
    {
        $request = Request::all();
        $params['first_type_id'] = $request['id'];
        $second_type = object_to_array(DB::table('second_type')->where(array("first_type_id" => $params['first_type_id']))->
        get(['id', 'name']));
        return json_encode($second_type, JSON_UNESCAPED_UNICODE);
    }
public function getSecondDate(){
    $request = Request::all();
    $id=$request['id'];
    $data=object_to_array(
        DB::table('second_type')->
        where(array("first_type_id"=>$id))->
        get(['id','name'])
    );
    return json_encode($data, JSON_UNESCAPED_UNICODE);
}
    public function getDate()
    {
        $uid = Session::get('uid');
        $request = Request::all();
        $second = $request['second'];
        $first = $request['first'];
        $secondIds = ota(DB::table($this->second_type_table)
            ->where(['first_type_id' => $first])
            ->pluck('id'));
        if(!in_array($second,$secondIds)){
            return json_return([]);
        }
        $item=Product::where(['second_type_id'=>$second])->where(['user_id'=>$uid])->whereNotNull('authority_time')->get();
        if(!empty($item)){
            return json_return(ota($item));
        }else{
            return json_return([]);
        }
       /* $res = object_to_array(
            DB::table('ipsell')->
            where(array("ipsell.user_id" => $uid, "ipsell.first_type" => $first, "ipsell.second_type" => $second, "ipsell.is_own" => 1))->
            join('product', 'ipsell.product_id', '=', 'product.id')->get(['product.id', 'product.name']));
        return json_encode($res, JSON_UNESCAPED_UNICODE);*/
    }

    public function addIpPay()
    {
        $input = Request::all();
        $uid = Session::get('uid');
        $data['user_id'] = $uid;
        $data['name'] = $input['name'];
        $data['need_markup'] = $input['is_authorize'];
        $data['ps'] = $input['supplement'];
        $data['min_price'] = $input['lowest_price'];
        $data['created_at'] = date('Y-m-d h:i:s');
        $data['updated_at'] = date('Y-m-d h:i:s');
        $data['first_type'] = $input['first'];
        $data['second_type'] = $input['second'];
        $data['progress'] = '7';
        if (!empty($data)) {
            $res = DB::table('ipbuy')->insert($data);
            if ($res) {
                return back()->with('success', '增添成功');

            } else {
                return back()->with('error', '增添失败');
            }
        } else {
            return back()->with('error', '暂无数据');
        }
    }

    public function editIpPay()
    {
        $input = Request::all();
        $uid = Session::get('uid');
        $id = $input['hide_id'];
        $data['user_id'] = $uid;
        $data['name'] = $input['edit_name'];
        $data['need_markup'] = $input['edit_is_authorize'];
        $data['ps'] = $input['edit_supplement'];
        $data['min_price'] = $input['edit_lowest_price'];
        $data['first_type'] = $input['edit_first_type'];
        $data['second_type'] = $input['edit_second_type'];
        $data['progress'] = $input['edit_progress'];
        if (!empty($data)) {
            $res = DB::table('ipbuy')->where(array("id" => $id))->update($data);
            if ($res) {
                return back()->with('success', '修改成功');
            } else {
                return back()->with('error', '修改失败');
            }
        } else {
            return back()->with('error', '暂无数据');
        }
    }

    public function delIpPay($id)
    {
        if (!empty($id)) {
            $res = DB::table('ipbuy')->where(array("id" => $id))->delete();
            if ($res) {
                return back()->with('success', '删除成功');
            } else {
                return back()->with('error', '删除失败');
            }
        } else {
            return back()->with('error', '数据传输错误,无法删除,请联系管理员');
        }
    }

    public function IpSell()
    {
        /*$uid = Session::get('uid');
        $ip_sell = object_to_array(DB::table('ipsell')->
        join('first_type', 'ipsell.first_type', '=', 'first_type.id')->
        join('second_type', 'ipsell.second_type', '=', 'second_type.id')->
        get(['ipsell.*', 'first_type.id as first_type_id', 'first_type.name as first_type',
            'second_type.id as second_type_id', 'second_type.name as second_type']));
        for ($i = 0; $i < count($ip_sell); $i++) {
            $ip_sell[$i]['product'] = object_to_array(
                DB::table('product')->where(array("id" => $ip_sell[$i]['product_id']))->first()
            );
            if (!empty($ip_sell[$i]['product'])) {
                $ip_sell[$i]['product_name'] = $ip_sell[$i]['product']['name'];
                $ip_sell[$i]['num'] = $ip_sell[$i]['product']['number'];
            }
        }
//        dump($ip_sell);exit();
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        $second_type = object_to_array(DB::table('second_type')->get(['id', 'name']));
        $product = object_to_array(DB::table('product')->where(array("user_id" => $uid))->
        get(['id', 'name']));
//        dump($product);exit();
        $data = [
            'ipSell' => $ip_sell,
            'first' => $first_type,
            'second' => $second_type,
            'product' => $product
        ];
        return view('user/ip/IpSell')->with('data', $data);*/
        $uid=Session::get('uid');
        $product=Ipsell::with(['FirstType','SecondType'])
            ->where(['user_id'=>$uid])
            ->paginate($this->pageNum);
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        $data = ['product','first_type'];
        //dump($product);exit;
        return view('user/ip/IpSell', compact($data));
    }
    public function IpSellSearch()
    {
        $uid=Session::get('uid');
        $request = Request::all();
        $keywords="";
        if (isset($request['search'])) {
            $keywords = $request['search'];
        }
        $product=Ipsell::with(['FirstType','SecondType'])
            ->where(['user_id'=>$uid])
            ->paginate($this->pageNum);
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        $data = ['product','first_type','keywords'];
        //dump($product);exit;
        return view('user/ip/IpSellSearch', compact($data));
    }

    public function IpSellCertain()
    {
        $request = Request::all();
        $uid=Session::get('uid');
        $first_type = $request['first_type'];
        $second_type = $request['second_type'];
        $name = $request['name'];
        $created_at = $request['created_at'];
        $progress = $request['progress'];
        $arr_province = ["不填","专利","商标","版权","项目"];
        $arr_second_type=config('config.arr_second_type');
        for ($j = 0;$j < sizeof($arr_second_type);$j++){
            if ($second_type == $arr_second_type[$j]){
                if ($j==0)
                    $j='';
                break;
            }
        }
        for ($i = 0;$i < 5;$i++){
            if ($first_type == $arr_province[$i]){
                if ($i==0)
                    $i='';
                break;
            }
        }
        // dd($request);
        $product=Ipsell::with(['FirstType','SecondType'])
            ->when($i, function ($query) use ($i) {
                return $query->where('first_type', $i);
            })
            ->when($j, function ($query) use ($j) {
                return $query->where('second_type', $j);
            })
            ->when($name, function ($query) use ($name) {
                return $query->where('product_name', $name);
            })
            ->when($created_at, function ($query) use ($created_at) {
                return $query->where('created_at','like','%'.$created_at.'%');
            })
            ->when($progress, function ($query) use ($progress) {
                return $query->where('progress', $progress);
            })
            ->where(['user_id'=>$uid])
            ->paginate($this->pageNum);
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        $data = ['product','first_type'];
        return view('user/ip/IpPurchaseSearch', compact($data));
    }

    public function addOwnIpSell()
    {
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        return view('user/ip/addOwnIpSell')->with('data', $first_type);
    }

    public function doAddOwnIpSell()
    {
        $input = Request::all();
        $uid = Session::get('uid');
        $data['product_id'] = $input['own_ip'];
        $data['user_id'] = $uid;
        $data['ideal_price'] = $input['ideal_price'];
        $data['min_price'] = $input['min_price'];
        $data['progress'] = '7';
        $data['second_type'] = $input['second'];
        $data['first_type'] = $input['first'];
        $data['is_authorization'] = $input['is_authorization'];
        $data['created_at'] = date('Y-m-d h:i:s');
        $data['updated_at'] = date('Y-m-d h:i:s');
        $data['is_own'] = 1;
        $res = DB::table('ipsell')->insert($data);
        if ($res) {
            return Redirect::to('user/ip/IpSell');
        } else {
            return back()->with('error', '添加失败，请联系管理员');
        }
    }

    public function addIpSell()
    {
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        return view('user/ip/addIpSell')->with('data', $first_type);
    }

    public function doAddIpSell()
    {
        $input = Request::all();
        $input = Request::all();
        $uid = Session::get('uid');
        $data['user_id'] = $uid;
        $data['product_name'] = $input['ip_name'];
        $data['ideal_price'] = $input['ideal_price'];
        $data['min_price'] = $input['min_price'];
        $data['progress'] = '7';
        $data['num'] = $input['ip_num'];
        $data['first_type'] = $input['first'];
        $data['second_type'] = $input['second'];
        $data['is_authorization'] = $input['is_authorization'];
        $data['created_at'] = date('Y-m-d h:i:s');
        $data['updated_at'] = date('Y-m-d h:i:s');
        $data['is_own'] = '0';
//        dump($data);exit();
        $res = DB::table('ipsell')->insert($data);
        if ($res) {
            return Redirect::to('user/ip/IpSell');
        } else {
            return back()->with('error', '添加失败，请联系管理员');
        }
    }

    public function delIpSell($id)
    {
        //dump($id);
        if ($id) {
            $del = DB::table('ipsell')->where(array("id" => $id))->delete();
            if ($del) {
                return back()->with('success', '删除成功');
            } else {
                return back()->with('error', '删除失败，请联系管理员');
            }
        } else {
            return back()->with('error', '数据传输有误，请联系管理员');
        }
    }

    public function editIpSell()
    {
        $input = Request::all();
        $data['ideal_price'] = $input['edit_expect_price'];
        $data['min_price'] = $input['edit_lowest_price'];
        $data['is_authorization'] = $input['edit_is_authorize'];
        if ($input['hide_is_own'] == 0) {
            $data['product_name'] = $input['edit_name'];
            $data['num'] = $input['edit_num'];
        }
        $id = $input['hide_id'];
        $res = DB::table('ipsell')->where(array("id" => $id))->update($data);
        if ($res) {
            return back()->with('success', '修改成功');
        } else {
            return back()->with('error', '修改失败，请联系管理员');
        }
//        dump($input);
    }
}